

#include "unity.h"
#include "unity_fixture.h"

#include "Log.h"
#include "BSP.h"
#include "BSP_Sleep.h"
#include "BitBuf.h"


TEST_GROUP(BitBuf);


TEST_SETUP(BitBuf)
{
	
}

TEST_TEAR_DOWN(BitBuf)
{
}



TEST(BitBuf, SetClearTest)
{
	uint32_t buffer[2] = {0};
	BitBuf_T bitbuf;
	int val;
	
	BitBuf_Init(&bitbuf, buffer, 2);
	TEST_ASSERT_EQUAL(0, BitBuf_TestBit(&bitbuf, 1, &val));
	TEST_ASSERT_EQUAL(0, val);
	
	TEST_ASSERT_EQUAL(0, BitBuf_SetBit(&bitbuf, 1));
	TEST_ASSERT_EQUAL(0, BitBuf_TestBit(&bitbuf, 1, &val));
	TEST_ASSERT_EQUAL(1, val);
	
	TEST_ASSERT_EQUAL(0, BitBuf_ClearBit(&bitbuf, 1));
	TEST_ASSERT_EQUAL(0, BitBuf_TestBit(&bitbuf, 1, &val));
	TEST_ASSERT_EQUAL(0, val);
}

TEST(BitBuf, SetBunld)
{
	uint32_t buffer[2] = {0x34, 0x56};
	BitBuf_T bitbuf;
	BitBundle_T bitbundle;
	int num;
	
	bitbundle.bundle = 0x32;
	bitbundle.off = 1;
	bitbundle.num = 8;
	
	BitBuf_Init(&bitbuf, buffer, 64);
	BitBuf_SetBundle(&bitbuf, 0, &bitbundle);
	TEST_ASSERT_BITS(0xFF, 0x32 >> 1, bitbuf.bundles[0]);
	bitbundle.off = 1;
	bitbuf.bundles[0] = 0;
	BitBuf_SetBundle(&bitbuf, 32, &bitbundle);
	TEST_ASSERT_BITS(0xFF, 0x32 >> 1, bitbuf.bundles[1]);
	bitbundle.off = 1;
	bitbuf.bundles[0] = bitbuf.bundles[1] = 0;
	num = BitBuf_SetBundle(&bitbuf, 28, &bitbundle);
	TEST_ASSERT_BITS(0xFF, (0x32 >> 1) & 0x0F, bitbuf.bundles[0]>>28);

	BitBuf_SetBundle(&bitbuf, 28+num, &bitbundle);
	TEST_ASSERT_BITS(0xFF, (0x32 >> 1) >> 4, bitbuf.bundles[1]);
}

TEST(BitBuf, GetBunld)
{
	uint32_t buffer[2] = {0x32 | (1 << 30) , 0x33};
	BitBuf_T bitbuf;
	BitBundle_T bitbundle;
	int num;
	
	bitbundle.bundle = 0;
	bitbundle.off = 0;
	bitbundle.num = 8;
	
	BitBuf_Init(&bitbuf, buffer, 64);

	num = BitBuf_GetBundle(&bitbuf, 0, &bitbundle);
	TEST_ASSERT_EQUAL(8, num);
	TEST_ASSERT_BITS(0xFF, buffer[0], bitbundle.bundle);
	
	bitbundle.bundle = 0;
	bitbundle.off = 0;
	bitbundle.num = 8;
	
	num = BitBuf_GetBundle(&bitbuf, 30, &bitbundle);
	TEST_ASSERT_EQUAL(2, num);
	TEST_ASSERT_BITS(0xFF, buffer[0]>>30, bitbundle.bundle);
	
	num = BitBuf_GetBundle(&bitbuf, 30+num, &bitbundle);
	TEST_ASSERT_EQUAL(6, num);
	TEST_ASSERT_BITS(0xFF >> 2, buffer[1], bitbundle.bundle >> 2);
}

TEST(BitBuf, GetSetBundleOver)
{
	uint32_t buffer[2] = {0x32 | (1 << 30) , 0x43};
	BitBuf_T bitbuf;
	BitBundle_T bitbundle;
	int num;
	
	bitbundle.bundle = 0;
	bitbundle.off = 0;
	bitbundle.num = 32;
	
	BitBuf_Init(&bitbuf, buffer, 64);

	num = BitBuf_SetBundle(&bitbuf, 33, &bitbundle);
	TEST_ASSERT_EQUAL(31, num);
	TEST_ASSERT_BITS(0xFFFFFFFF, 0x01, buffer[1]);
	
	bitbundle.bundle = 0;
	bitbundle.off = 0;
	bitbundle.num = 32;
	
	BitBuf_Init(&bitbuf, buffer, 32);
	num = BitBuf_GetBundle(&bitbuf, 2, &bitbundle);
	TEST_ASSERT_EQUAL(30, num);
	TEST_ASSERT_BITS(0xFFFFFF, 0x0C, buffer[0] >> 2);
}



